<?php

  //////////////////////////////////////////////////////////////////
  // OrbitFAQ                                                     //
  // ---------                                                    //
  //                                                              //
  // Orbit FAQ was solely written and developed by Orbit Services //
  // http://www.orbitservices.net                                 //
  //                                                              //
  // Access the Forum here:                                       //
  // http://forums.orbitservices.net/index.php?c=4                //
  //                                                              //
  // OrbitFAQ utilises the following opensource projects/classes; //
  //  + Fckeditor - http://www.fckeditor.net                      //
  //  + Smarty Template Engine - http://smarty.php.net            //
  //  + Swift Email Class - http://www.swiftmailer.org/           //
  //  + OWASP PHP Filter Project - http://www.owasp.org           //
  //  + MySQL Search Class by Stephen Bartholomew                 //
  //                                                              //
  //////////////////////////////////////////////////////////////////

  $smarty->assign("OrbitFAQTitle","FAQ Statistics");

  if($action != '')
    {
      if($action == 'clear')
        {

          $smarty_template_file = "$AdminFullPath/skins/$AdminSkin/mod_". $mod ."_". $action .".tpl";

          if($posted == 'yes')
            {

              $posted_confirmation = sanitize_paranoid_string($_POST['posted_confirmation']);

              if($posted_confirmation == '0'){ $error .= " &raquo; You did not select an appropriate <u>Confirmation</u><br />"; }

              if(!$error)
                {

                  // Delete Our Msg
                  $query_delete = "DELETE FROM `orbitfaq_stats`;";
                  $result_delete = $faqsql_query ($query_delete)OR DIE( "$sql_query_error $query_delete");

                  clearSmartyCompiled($SmartyCompileDir, $ClearSmartyCompiled);

                  $message = "Mailbox was Successfully Cleared!";
                  header("Location: index.php?mod=$mod&message=$message");

                  exit;

                }

              $smarty->assign('Errors',$error);


            }


        } // End Clear
      }

    else
      {
          $statLIMIT = '10';

          $smarty_template_file = "$AdminFullPath/skins/$AdminSkin/mod_$mod.tpl";

          // Total Page Hits
          $query_hits = "SELECT id FROM orbitfaq_stats;";
          $result_hits = $faqsql_query ($query_hits)OR DIE( "$sql_query_error $query_hits");
          $count_hits = $faqsql_count_rows($result_hits);

          $smarty->assign('TotalPageHites',$count_hits);

          // Total Unique Page Hits by phpsessid
          $query_uhits = "SELECT phpsessid FROM orbitfaq_stats GROUP BY `phpsessid`;";
          $result_uhits = $faqsql_query ($query_uhits)OR DIE( "$sql_query_error $query_uhits");
          $count_uhits = $faqsql_count_rows($result_uhits);

          $smarty->assign('TotalUniquePageHites',$count_uhits);

          // Top 20 Most Popular Pages
          $query_poppgs = "SELECT full_url, COUNT(*) as hits FROM orbitfaq_stats GROUP BY `full_url` ORDER BY `hits` DESC LIMIT $statLIMIT";
          $result_poppgs = $faqsql_query ($query_poppgs)OR DIE( "$sql_query_error $query_poppgs");

          while ($row_poppgs = $faqsql_fetch_array ($result_poppgs)){
                $poppgs_full_url = $row_poppgs[0];
                $poppgs_count = $row_poppgs[1];

                if($row_class == '1')
                  {
                    $row_class = '0';
                  }
                else
                  {
                    $row_class = '1';
                  }

                 $smarty_poppgs_array[] = array(
                    "poppgs_full_url" => $poppgs_full_url,
                    "poppgs_count" => $poppgs_count,
                    "row_class" => $row_class
                  );
          }

          $smarty->assign('PopularPagesListing',$smarty_poppgs_array);

          // Top 20 Most Popular Browsers
          $query_popagent = "SELECT user_agent, COUNT(*) as hits FROM orbitfaq_stats GROUP BY `user_agent` ORDER BY `hits` DESC LIMIT $statLIMIT";
          $result_popagent = $faqsql_query ($query_popagent)OR DIE( "$sql_query_error $query_popagent");

          while ($row_popagent = $faqsql_fetch_array ($result_popagent)){
                $popagent_agent = $row_popagent[0];
                $popagent_count = $row_popagent[1];

                if($row_class == '1')
                  {
                    $row_class = '0';
                  }
                else
                  {
                    $row_class = '1';
                  }

                 $smarty_popagent_array[] = array(
                    "popagent_agent" => $popagent_agent,
                    "popagent_count" => $popagent_count,
                    "row_class" => $row_class
                  );
          }

          $smarty->assign('PopularBrowsersListing',$smarty_popagent_array);

          // Top 20 Most FAQS
          $query_popfaq = "SELECT f_id, COUNT(*) as hits FROM orbitfaq_stats WHERE f_id != '0' GROUP BY f_id ORDER BY `hits` DESC LIMIT $statLIMIT";
          $result_popfaq = $faqsql_query ($query_popfaq)OR DIE( "$sql_query_error $query_popfaq");

          while ($row_popfaq = $faqsql_fetch_array ($result_popfaq)){
            $popfaq_id = $row_popfaq[0];
            $popfaq_count = $row_popfaq[1];

            $query_popfaq_title = "SELECT title FROM orbitfaq WHERE id = '$popfaq_id' LIMIT 1";
            $result_popfaq_title = $faqsql_query ($query_popfaq_title)OR DIE( "$sql_query_error $query_popfaq_title");

            while ($row_popfaq_title = $faqsql_fetch_array ($result_popfaq_title)){
              $popfaq_title = $row_popfaq_title[0];
            }

                if($row_class == '1')
                  {
                    $row_class = '0';
                  }
                else
                  {
                    $row_class = '1';
                  }

                 $smarty_popfaq_array[] = array(
                    "popfaq_title" => $popfaq_title,
                    "popfaq_count" => $popfaq_count,
                    "row_class" => $row_class
                  );
          }

          $smarty->assign('PopularFAQListing',$smarty_popfaq_array);

          // Top 20 Most Categories
          $query_popcat = "SELECT c_id, f_id, COUNT(*) as hits FROM orbitfaq_stats WHERE f_id != '0' AND c_id != '0' GROUP BY c_id ORDER BY `hits` DESC LIMIT $statLIMIT";
          $result_popcat = $faqsql_query ($query_popcat)OR DIE( "$sql_query_error $query_popcat");

          while ($row_popcat = $faqsql_fetch_array ($result_popcat)){
            $popcat_id = $row_popcat[0];
            $popcat_fid = $row_popcat[1];
            $popcat_count = $row_popcat[2];

            $query_popcat_c_title = "SELECT title FROM orbitfaq_categories WHERE id = '$popcat_id' LIMIT 1";
            $result_popcat_c_title = $faqsql_query ($query_popcat_c_title)OR DIE( "$sql_query_error $query_popcat_c_title");

            while ($row_popcat_c_title = $faqsql_fetch_array ($result_popcat_c_title)){
              $popcat_c_title = $row_popcat_c_title[0];
            }

            $query_popcat_f_title = "SELECT title FROM orbitfaq WHERE id = '$popcat_fid' LIMIT 1";
            $result_popcat_f_title = $faqsql_query ($query_popcat_f_title)OR DIE( "$sql_query_error $query_popcat_f_title");

            while ($row_popcat_f_title = $faqsql_fetch_array ($result_popcat_f_title)){
              $popcat_f_title = $row_popcat_f_title[0];
            }

                if($row_class == '1')
                  {
                    $row_class = '0';
                  }
                else
                  {
                    $row_class = '1';
                  }

                 $smarty_popcat_array[] = array(
                    "popcat_ftitle" => $popcat_f_title,
                    "popcat_title" => $popcat_c_title,
                    "popcat_count" => $popcat_count,
                    "row_class" => $row_class
                  );
          }

          $smarty->assign('PopularCATListing',$smarty_popcat_array);

      }

?>